package com.hdoit.ares.web.cust.wechat;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.cust.model.CustCollection;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustCollectionService;
import com.hdoit.ares.logical.official.model.Artical;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.wechat.BaseWechat;

@Controller
@RequestMapping("/wechat/cust/colAndFoot")
public class CustCollectionWechat extends BaseWechat{

	@Override
	public String getRoutePath() {
		
		return "areas/wechat/cust/colAndFoot/";
	}

	private ICustCollectionService custCollectionService;
	
	public ICustCollectionService getCustCollectionService() {
		return custCollectionService;
	}
	
	@Resource
	public void setCustCollectionService(
			ICustCollectionService custCollectionService) {
		this.custCollectionService = custCollectionService;
	}
	 
	private IWithdrawParamService withdrawParamService;

	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}

	@Resource
	public void setWithdrawParamService(IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}

	private final String addCollectViewNameString = "addCollect";
	private final String deleteViewName = "delete";
	private final String isCollectViewName = "isCollect";
	private final String collectionListViewName = "collectionList";/*收藏列表*/
	
	@RequestMapping(value=collectionListViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String collectionList(Model model,String type,Integer pageNum){
		System.err.println("------------------------"+type);
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel); 
		Customer customer  = (Customer)loginModel.get(AresManageContainer.customer_account_info);
		WithdrawParam wp = withdrawParamService.queryModelByHelpCode("pageSize");
		Integer length = wp.getParam();
		System.err.println("----传过来的页数-------"+pageNum);
		List<Artical> articals=custCollectionService.selectArticalByType(type, customer.getId(),pageNum,length);
		
		List<Artical> allArticals=custCollectionService.selectArticalByType(type, customer.getId(),null,null);
		int allPage = (int)Math.ceil((double)allArticals.size()/(double)length);
		System.err.println("---------------------------"+allPage);
		
		model.addAttribute("allPage",allPage);
		model.addAttribute("articals",articals);
		return view(collectionListViewName);
	}
	
	@ResponseBody
	@RequestMapping(value=isCollectViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String isCollection(Model model,String type,String thingId){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel); 
		Customer customer  = (Customer)loginModel.get(AresManageContainer.customer_account_info);
		
		CustCollection  custCollection = new CustCollection();
		custCollection.setType(type);
		custCollection.setThingId(thingId);
		custCollection.setCustId(customer.getId());
		
		CustCollection CustResult = custCollectionService.queryModel(custCollection);
		String result = "false";
		
		if(CustResult!=null){
			result ="true";
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value=addCollectViewNameString,method={RequestMethod.GET,RequestMethod.POST})
	public String addCollect(Model model,String type,String thingId){
		
		System.err.println("--------------------"+type);
		System.err.println("--------------------"+thingId);
		
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel); 
		Customer customer  = (Customer)loginModel.get(AresManageContainer.customer_account_info);
		
		CustCollection custCollection=custCollectionService.selectCustCollection(thingId, customer.getId());
		boolean result;
		if(custCollection == null){
			custCollection = new CustCollection();
			custCollection.setId(CommonUtils.getKeyId());
			custCollection.setType(type);
			custCollection.setThingId(thingId);
			custCollection.setCustId(customer.getId());
			custCollection.setCollectionDate(DateUtils.getCurrent());
			
			custCollection.setModelStatus(ModelStatus.add);
			result = custCollectionService.applyChange(custCollection);
		}else {
			result = false;
		}
		
		return String.valueOf(result);
	}
	
	@ResponseBody
	@RequestMapping(value=deleteViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String delete(Model model,String type,String thingId){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel); 
		Customer customer  = (Customer)loginModel.get(AresManageContainer.customer_account_info);
		
		int result = custCollectionService.deleteCollection(type, thingId, customer.getId());
	
		if(result>0){
			return "true";
		}else{
			return "false";
		}
	}
	
}
